<template>
  <div id="app">

    <el-input v-model="form.itemBrand" placeholder="请输入品牌名称"></el-input>
    <el-input v-model="form.itemName" placeholder="请输入商品名称"></el-input>
    <el-input v-model="form.itemPrice" placeholder="请输入价格"></el-input>
    <el-date-picker
      v-model="value2"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions"
       format="yyyy 年 MM 月 dd 日"
      value-format="yyyy-MM-dd"
    ></el-date-picker>
      <el-button type="primary" @click="select()">查询</el-button>
      <el-button type="primary" @click="myShoppingCar()">我的购物车</el-button>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="id" label="id" width="200"></el-table-column>
      <el-table-column prop="itemName" label="商品名称" width="200"></el-table-column>
      <el-table-column prop="itemImage" label="商品图片" width="200"></el-table-column>
      <el-table-column prop="itemDesc" label="商品描述" width="200"></el-table-column>
      <el-table-column prop="itemPrice" label="价格" width="300"></el-table-column>
      <el-table-column prop="itemBrand" label="品牌" width="300"></el-table-column>
      <el-table-column prop="itemColor" label="颜色" width="200"></el-table-column>
      <el-table-column prop="itemSales" label="销量" width="200"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200"></el-table-column>

      <el-table-column label="数量">
        <template slot-scope="scope">
               <el-input  v-model="scope.row.sum" placeholder="请输入数量"></el-input>
          </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="addCar(scope.row)" type="text" size="small">添加购物车</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  mounted(){
    this.getRequest();
  },
  methods: {
    select(){
        console.log(this.value2);
        console.log(this.form);
        this.getRequest();
    },
    getRequest() {
       this.form.pageNo = this.pageNo;
       this.form.pageSize = this.pageSize;
       this.form.startTime = '';
       this.form.endTime = '';
        if(this.value2!=null){
          this.form.startTime = this.value2[0];
          this.form.endTime = this.value2[1];
        }
      var $this = this;
      this.axios({
        method: "get",
        url: "http://localhost:9000/goods/item/list",
        params: this.form
      }).then(function(res){
        $this.tableData = res.data.obj.content;
        $this.total = res.data.obj.totalElements;
      });
    },
    myShoppingCar(){
        this.$router.replace({path:"/myShoppingCar"});
    },
    addCar(row) {
      var params = {"itemId":row.id,"sum":row.sum}
      console.log(row);
      var $this = this;
      this.axios({
        method: "post",
        url: "http://localhost:9000/shoppingcar/car/add",
        params: params
      }).then(function(res){
        alert(res.data.msg);
        console.log("------------------>{}",res);
        if(res.data.code=='401'){
          //跳转到登录页面
          $this.$router.replace({path:"/Login"});
        }
        
      });

    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getRequest();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.getRequest();
    }
  },

  data() {
    return {
      tableData: [],
      pageNo: 1,
      pageSize: 2,
      total: 100,
      value2: '',
      form:{
        itemName:'',
        itemPrice:'',
        itemBrand:'',
        startTime:'',
        endTime:'',
        pageNo:1,
        pageSize:2
      },
      sum:null,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  }
};
</script>